package gs.wicket.app;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Properties;

import org.apache.wicket.Application;
import org.apache.wicket.Page;
import org.apache.wicket.Request;
import org.apache.wicket.Response;
import org.apache.wicket.Session;
import org.apache.wicket.protocol.http.WebApplication;

import gs.wicket.app.product.Product;
import gs.wicket.app.session.GSSession;
import gs.wicket.app.home.HomePage;

public class GSApp extends WebApplication {

	 private static List<Product> products = new ArrayList<Product>();
	 private static List<Product> wproducts = new ArrayList<Product>();
	 private static List<Product> parproducts = new ArrayList<Product>();
	 private static List<Product> dproducts = new ArrayList<Product>();
	 private static List<Product> jproducts = new ArrayList<Product>();
	 private static List<Product> productsEn = new ArrayList<Product>();
	 private static List<Product> wproductsEn = new ArrayList<Product>();
	 private static List<Product> parproductsEn = new ArrayList<Product>();
	 private static List<Product> dproductsEn = new ArrayList<Product>();
	 private static List<Product> jproductsEn = new ArrayList<Product>();
	 
	 public GSApp(){
		 
	 }
	 
	 @Override
	  protected void init() {
	    super.init();

	    // read the list of products from a properties file
	    readProducts();
	    readWatchProducts();
	    readParfumProducts();
	    readDrinkProducts();
	    readJewelryProducts();
	    
	  }
	 
	 public static GSApp get() {
		    return (GSApp) Application.get();
		  }
	  @Override
	  public Class<? extends Page> getHomePage() {
	    return HomePage.class;
	  }
	  
	  @Override
	  public Session newSession(Request request, Response response) {
	    return new GSSession(request);
	  }

	  public static List<Product> getProducts(String lang) {
		  if(lang=="ba")
			  return Collections.unmodifiableList(products);
		  else
			  return Collections.unmodifiableList(productsEn);
		  }

	  public static List<Product> getWatchProducts(String lang) {
		  if(lang=="ba")
			  return Collections.unmodifiableList(wproducts);
		  else
			  return Collections.unmodifiableList(wproductsEn); 
		  }
	  
	  public static List<Product> getParfumProducts(String lang) {
		  if(lang=="ba")
			  return Collections.unmodifiableList(parproducts);
		  else
			  return Collections.unmodifiableList(parproductsEn);
		  }
	  
	  public static List<Product> getDrinkProducts(String lang) {
		  if(lang=="ba")
			  return Collections.unmodifiableList(dproducts);
		  else
			  return Collections.unmodifiableList(dproducts);
		  }
	  
	  public static List<Product> getJewelryProducts(String lang) {
		  if(lang=="ba")
			  return Collections.unmodifiableList(jproducts);
		  else
			  return Collections.unmodifiableList(jproductsEn);
		  }
		  
		  ////postavljen static
		  
		  private static void readProducts() {
		    Properties props = new Properties();
		    Properties propsEn = new Properties();
		    try {		    	
		      props.load(GSApp.class.getResourceAsStream("products.properties"));
		      propsEn.load(GSApp.class.getResourceAsStream("products_en.properties"));
		      
		    } catch (IOException e) {
		      e.printStackTrace();
		    }

		    for (Object obj : props.keySet()) {
		      String key = obj.toString();

		      
		      if (!key.endsWith(".name"))
		        continue;
		      key = key.substring(0, key.indexOf("."));

		      // retrieve each property value
		      String name = props.getProperty(key + ".name");
		      String description = props.getProperty(key + ".description");
		      String longDescription = props.getProperty(key + ".longDescription");
		      double price = Double.valueOf(props.getProperty(key + ".price"));
		      String imageURL =  props.getProperty(key + ".imageURL");
		      String comments = props.getProperty(key + ".comments");

		      products.add(new Product(name, description, longDescription, imageURL, price, comments));
		    }
		    
		    for (Object obj : propsEn.keySet()) {
			      String key = obj.toString();

			      
			      if (!key.endsWith(".name"))
			        continue;
			      key = key.substring(0, key.indexOf("."));

			      // retrieve each property value
			      String name = propsEn.getProperty(key + ".name");
			      String description = propsEn.getProperty(key + ".description");
			      String longDescription = propsEn.getProperty(key + ".longDescription");
			      double price = Double.valueOf(propsEn.getProperty(key + ".price"));
			      String imageURL =  propsEn.getProperty(key + ".imageURL");
			      String comments = propsEn.getProperty(key + ".comments");

			      productsEn.add(new Product(name, description, longDescription, imageURL, price, comments));
			    }
		  }
		  private static void readWatchProducts() {
			    Properties props = new Properties();
			    Properties propsEn = new Properties();
			    try {
			      props.load(GSApp.class.getResourceAsStream("watchproducts.properties"));
			      propsEn.load(GSApp.class.getResourceAsStream("watchproducts_en.properties"));
			      
			    } catch (IOException e) {
			      e.printStackTrace();
			    }

			    for (Object obj : props.keySet()) {
			      String key = obj.toString();

			      // only process a cheese once (identified by its name)
			      if (!key.endsWith(".name"))
			        continue;
			      key = key.substring(0, key.indexOf("."));

			      // retrieve each property value
			      String name = props.getProperty(key + ".name");
			      String description = props.getProperty(key + ".description");
			      String longDescription = props.getProperty(key + ".longDescription");
			      double price = Double.valueOf(props.getProperty(key + ".price"));
			      String imageURL =  props.getProperty(key + ".imageURL");
			      String comments = props.getProperty(key + ".comments");

			      wproducts.add(new Product(name, description, longDescription, imageURL, price, comments));
			    }
			    
			    for (Object obj : propsEn.keySet()) {
				      String key = obj.toString();
				      
				      if (!key.endsWith(".name"))
				        continue;
				      key = key.substring(0, key.indexOf("."));
				      
				      String name = propsEn.getProperty(key + ".name");
				      String description = propsEn.getProperty(key + ".description");
				      String longDescription = propsEn.getProperty(key + ".longDescription");
				      double price = Double.valueOf(propsEn.getProperty(key + ".price"));
				      String imageURL =  propsEn.getProperty(key + ".imageURL");
				      String comments = propsEn.getProperty(key + ".comments");

				      wproductsEn.add(new Product(name, description, longDescription, imageURL, price, comments));
				    }
			  }
		  private static void readParfumProducts() {
			    Properties props = new Properties();
			    Properties propsEn = new Properties();
			    try {
			      props.load(GSApp.class.getResourceAsStream("parfumsproducts.properties"));
			      propsEn.load(GSApp.class.getResourceAsStream("parfumsproducts_en.properties"));
			    } catch (IOException e) {
			      e.printStackTrace();
			    }

			    for (Object obj : props.keySet()) {
			      String key = obj.toString();

			      // only process a cheese once (identified by its name)
			      if (!key.endsWith(".name"))
			        continue;
			      key = key.substring(0, key.indexOf("."));

			      // retrieve each property value
			      String name = props.getProperty(key + ".name");
			      String description = props.getProperty(key + ".description");
			      String longDescription = props.getProperty(key + ".longDescription");
			      double price = Double.valueOf(props.getProperty(key + ".price"));
			      String imageURL =  props.getProperty(key + ".imageURL");
			      String comments = props.getProperty(key + ".comments");

			      parproducts.add(new Product(name, description, longDescription, imageURL, price, comments));
			    }
			    
			    for (Object obj : propsEn.keySet()) {
				      String key = obj.toString();

				      // only process a cheese once (identified by its name)
				      if (!key.endsWith(".name"))
				        continue;
				      key = key.substring(0, key.indexOf("."));

				      // retrieve each property value
				      String name = propsEn.getProperty(key + ".name");
				      String description = propsEn.getProperty(key + ".description");
				      String longDescription = propsEn.getProperty(key + ".longDescription");
				      double price = Double.valueOf(propsEn.getProperty(key + ".price"));
				      String imageURL =  propsEn.getProperty(key + ".imageURL");
				      String comments = propsEn.getProperty(key + ".comments");

				      parproductsEn.add(new Product(name, description, longDescription, imageURL, price, comments));
				    }

			  }
		  private static void readDrinkProducts() {
			    Properties props = new Properties();
			    Properties propsEn = new Properties();
			    try {
			      props.load(GSApp.class.getResourceAsStream("drinkproducts.properties"));
			      propsEn.load(GSApp.class.getResourceAsStream("drinkproducts_en.properties"));
			    } catch (IOException e) {
			      e.printStackTrace();
			    }

			    for (Object obj : props.keySet()) {
			      String key = obj.toString();

			      // only process a cheese once (identified by its name)
			      if (!key.endsWith(".name"))
			        continue;
			      key = key.substring(0, key.indexOf("."));

			      // retrieve each property value
			      String name = props.getProperty(key + ".name");
			      String description = props.getProperty(key + ".description");
			      String longDescription = props.getProperty(key + ".longDescription");
			      double price = Double.valueOf(props.getProperty(key + ".price"));
			      String imageURL =  props.getProperty(key + ".imageURL");
			      String comments = props.getProperty(key + ".comments");

			      dproducts.add(new Product(name, description, longDescription, imageURL, price, comments));
			    }
			    
			    for (Object obj : propsEn.keySet()) {
				      String key = obj.toString();

				      // only process a cheese once (identified by its name)
				      if (!key.endsWith(".name"))
				        continue;
				      key = key.substring(0, key.indexOf("."));

				      // retrieve each property value
				      String name = propsEn.getProperty(key + ".name");
				      String description = propsEn.getProperty(key + ".description");
				      String longDescription = propsEn.getProperty(key + ".longDescription");
				      double price = Double.valueOf(propsEn.getProperty(key + ".price"));
				      String imageURL =  propsEn.getProperty(key + ".imageURL");
				      String comments = propsEn.getProperty(key + ".comments");

				      dproductsEn.add(new Product(name, description, longDescription, imageURL, price, comments));
				    }

			  }
		  private static void readJewelryProducts() {
			    Properties props = new Properties();
			    Properties propsEn = new Properties();
			    try {
			      props.load(GSApp.class.getResourceAsStream("jewelryproducts.properties"));
			      propsEn.load(GSApp.class.getResourceAsStream("jewelryproducts_en.properties"));
			    } catch (IOException e) {
			      e.printStackTrace();
			    }

			    for (Object obj : props.keySet()) {
			      String key = obj.toString();

			      // only process a cheese once (identified by its name)
			      if (!key.endsWith(".name"))
			        continue;
			      key = key.substring(0, key.indexOf("."));

			      // retrieve each property value
			      String name = props.getProperty(key + ".name");
			      String description = props.getProperty(key + ".description");
			      String longDescription = props.getProperty(key + ".longDescription");
			      double price = Double.valueOf(props.getProperty(key + ".price"));
			      String imageURL =  props.getProperty(key + ".imageURL");
			      String comments = props.getProperty(key + ".comments");

			      jproducts.add(new Product(name, description, longDescription, imageURL, price, comments));
			    }
			    
			    for (Object obj : propsEn.keySet()) {
				      String key = obj.toString();

				      // only process a cheese once (identified by its name)
				      if (!key.endsWith(".name"))
				        continue;
				      key = key.substring(0, key.indexOf("."));

				      // retrieve each property value
				      String name = propsEn.getProperty(key + ".name");
				      String description = propsEn.getProperty(key + ".description");
				      String longDescription = propsEn.getProperty(key + ".longDescription");
				      double price = Double.valueOf(propsEn.getProperty(key + ".price"));
				      String imageURL =  propsEn.getProperty(key + ".imageURL");
				      String comments = propsEn.getProperty(key + ".comments");

				      jproductsEn.add(new Product(name, description, longDescription, imageURL, price, comments));
				    }

			  }
		  
	  public static void writeProductComment(Product product, String newName, String newComm) {
		    Properties props = new Properties();
		    System.out.println("variableone");
		    try {
		       //props.load(new FileInputStream("products.properties"));
		    	props.load(GSApp.class
				          .getResourceAsStream("products.properties"));
		    } catch (IOException e) {
		      e.printStackTrace();
		    }

		    for (Object obj : props.keySet()) {
		      String key = obj.toString();

		      // only process a cheese once (identified by its name)
		      if (!key.endsWith(".name"))
		        continue;
		      key = key.substring(0, key.indexOf("."));

		      //
		      if(props.getProperty(key + ".name").toString().equals(product.getName())){
		    	  // rev112 
		    	  String prevComm = props.getProperty(key + ".comments");
		    	  
		    	  String returnString =  product.getCurrentComments() + " <div id=\"commentName\">" + newName + ":</div>"+
  			  " <div id=\"commentText\"><div class=\"w1\"><div class=\"w2\"><div class=\"w3\"><div class=\"w4\">" + 
  			  newComm + "</div></div></div></div></div>";
		    	  
		    	  product.setCurrentComments(returnString);
		    	  
		    	  props.setProperty(key + ".comments", prevComm + returnString);
		    	  break;}
		    	}
		      try {
		    	  FileOutputStream outputStream = new FileOutputStream("./src/gs/wicket/app/products.properties");
		    	props.store(outputStream, " ");
		    	outputStream.flush();
		    	outputStream.close();
			  } catch (IOException e) {
			      e.printStackTrace();
			  }
		  }
 public static List<Product> findProducts(String srchstring) {
		  
		  List<Product> foundProducts = new ArrayList<Product>();
		  
		  Properties props = new Properties();
		    try {
		      props.load(GSApp.class
		          .getResourceAsStream("products.properties"));
		    } catch (IOException e) {
		      e.printStackTrace();
		    }

		    for (Object obj : props.keySet()) {
		      String key = obj.toString();

		      // only process a cheese once (identified by its name)
		      if (!key.endsWith(".name"))
		        continue;
		      key = key.substring(0, key.indexOf("."));
		      //novo
		      String nefsd;
		  
		      if(((props.getProperty(key + ".description")).toLowerCase()).contains(srchstring.toLowerCase())){
		    	  // retrieve each property value
		    	  String name = props.getProperty(key + ".name");
		    	  String description = props.getProperty(key + ".description");
		    	  String longDescription = props.getProperty(key + ".longDescription");
		    	  double price = Double.valueOf(props.getProperty(key + ".price"));
		    	  String imageURL =  props.getProperty(key + ".imageURL");
		    	  String comments = props.getProperty(key + ".comments");
		    	  foundProducts.add(new Product(name, description, longDescription, imageURL, price, comments));
		      }
		    }
		    return foundProducts;
	  }
}
